package com.bd.scm.module.user.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bd.offices.excel.ExcelUtil;
import com.bd.scm.dao.mapper.login.LoginMapper;
import com.bd.scm.dao.mapper.user.DtUserMapper;
import com.bd.scm.dao.mapper.user.TransferUserMapper;
import com.bd.scm.domain.dto.user.PersonnelFilesDto;
import com.bd.scm.domain.dto.user.UserOfLeaveBatchDto;
import com.bd.scm.domain.models.department.DtDepCust;
import com.bd.scm.domain.models.user.DtUser;
import com.bd.scm.domain.models.user.PersonnelFilesImport;
import com.bd.scm.domain.models.user.TransferUser;
import com.bd.scm.module.department.service.IDtDepCustService;
import com.bd.scm.module.enums.ExceptionEmun;
import com.bd.scm.module.exception.ScmException;
import com.bd.scm.module.login.service.LoginService;
import com.bd.scm.module.user.service.IDtUserService;
import com.bd.utils.DateUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.io.BufferedInputStream;
import java.io.InputStream;
import java.net.InetAddress;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * 员工服务实现类
 * User: _Cps
 * Date: 2019.08.28 16:44
 */
@Service
@Slf4j
@Transactional
public class DtUserServiceImpl extends ServiceImpl<DtUserMapper, DtUser> implements IDtUserService {

    /**
     * 登录Service
     */
    @Autowired
    private LoginMapper loginMapper;

    @Autowired
    private DtUserMapper dtUserMapper;

    @Autowired
    private IDtDepCustService iDtDepCustService;

    @Resource
    private TransferUserMapper transferUserMapper;

    /**
     * 获取人员（包含假期额度数据）.
     *
     * @param userOfLeaveBatchDto
     * @return
     */
    @Override
    public List<UserOfLeaveBatchDto> getUserInfoList(UserOfLeaveBatchDto userOfLeaveBatchDto) {
        return dtUserMapper.getUserInfoList(userOfLeaveBatchDto);
    }
    /**
     * 获取人员（结合工作日历）.
     *
     * @param userOfLeaveBatchDto
     * @return
     */
    @Override
    public List<UserOfLeaveBatchDto> getUserCalendarInfoList(UserOfLeaveBatchDto userOfLeaveBatchDto) {
        return dtUserMapper.getUserCalendarInfoList(userOfLeaveBatchDto);
    }

    /**
     * 根据用户ID检查是否存在
     *
     * @param userSerial
     * @return
     */
    @Override
    public boolean checkExistById(Long userSerial) {
        QueryWrapper<DtUser> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_serial", userSerial);
        return dtUserMapper.selectOne(queryWrapper) != null;
    }

    /**
     * 根据身份证号码检查是否存在
     *
     * @param idCard
     * @return
     */
    @Override
    public boolean checkExistByIdCard(String idCard) {
        return getByIdCard(idCard) != null;
    }

    /**
     * 根据身份证号码获取员工信息
     *
     * @param idCard
     * @return
     */
    @Override
    public DtUser getByIdCard(String idCard) {
        QueryWrapper<DtUser> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id", idCard);
        return dtUserMapper.selectOne(queryWrapper);
    }



    @Override
    public IPage<PersonnelFilesDto> getUserList(Page<PersonnelFilesDto> page, PersonnelFilesDto personnelFilesDto) {
        return dtUserMapper.getUserList(page, personnelFilesDto);
    }
    @Override
    public List<PersonnelFilesDto> getManualUserList(PersonnelFilesDto personnelFilesDto) {
        return dtUserMapper.getManualUserList(personnelFilesDto);
    }


    @Override
    @Transactional
    public PersonnelFilesDto addUserInfo(PersonnelFilesDto personnelFilesDto, String clientIp) {
        //返回信息字符串
//        int r = 0;

        if (!StringUtils.isEmpty(personnelFilesDto.getUserNo())) {
            personnelFilesDto.setUserNo(String.valueOf(personnelFilesDto.getUserNo()));
            int userNoCount = dtUserMapper.getUserNo(personnelFilesDto);

            if (userNoCount > 0) {
                throw new ScmException(ExceptionEmun.USER_ERROR_NAMEREPETITION);
            }
        }
        int userIdCount = dtUserMapper.getUserId(personnelFilesDto);
        if (userIdCount > 0) {
            throw new ScmException(ExceptionEmun.USER_ERROR_USERID);
        }

        if (StringUtils.isEmpty(personnelFilesDto.getUserNo())) {
            dtUserMapper.updateUserSerial();
            Long user_serial = dtUserMapper.getUserSerialAndNo().getModuleUser();
            personnelFilesDto.setUserNo(String.valueOf(user_serial));
            personnelFilesDto.setUserSerial(user_serial);
        } else {
            personnelFilesDto.setUserSerial(Long.parseLong(personnelFilesDto.getUserNo()));
        }
        HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();

        JSONObject user = (JSONObject) request.getSession().getAttribute("user");
        String operator =String.valueOf(user.get("userSerial"));
        personnelFilesDto.setGlyNo(operator);

        PersonnelFilesDto depInfo = dtUserMapper.getDepInfo(personnelFilesDto);
        personnelFilesDto.setDepNo(depInfo.getDepNo());
        personnelFilesDto.setDepName(depInfo.getDepName());
        personnelFilesDto.setLogIp(clientIp);
        //添加人员全年的普通班次 除了封账月份
        personnelFilesDto.setDoType(0);
        personnelFilesDto.setUserSerialStr(personnelFilesDto.getUserSerial()+"");
        dtUserMapper.addScheduleCalendarPuTong(personnelFilesDto);
        dtUserMapper.insertTmpDtUser(personnelFilesDto);
        dtUserMapper.insertVacation(personnelFilesDto);
        dtUserMapper.insertDtAcUser(personnelFilesDto);
        dtUserMapper.insertDtAcLink(personnelFilesDto);
        String user_lname = personnelFilesDto.getUserLname();
        String user_no = personnelFilesDto.getUserNo();
        personnelFilesDto.setLogBz("姓名:" + user_lname + "  工号:" + user_no);
        dtUserMapper.insertUserLogByAdd(personnelFilesDto);
        dtUserMapper.insertKqfxRiZhi(personnelFilesDto);
        dtUserMapper.insertWtPublicByAdd(personnelFilesDto);
        //计算授乳假
        calculateLactational(personnelFilesDto);
//        r = 1;
        return personnelFilesDto;
    }

    @Override
    @Transactional
    public PersonnelFilesDto addUserInfoByFace(PersonnelFilesDto personnelFilesDto, String clientIp) {
        if (!StringUtils.isEmpty(personnelFilesDto.getUserNo())) {
            personnelFilesDto.setUserNo(String.valueOf(personnelFilesDto.getUserNo()));
            int userNoCount = dtUserMapper.getUserNo(personnelFilesDto);

            if (userNoCount > 0) {
                throw new ScmException(ExceptionEmun.USER_ERROR_NAMEREPETITION);
            }
        }
        int userIdCount = dtUserMapper.getUserId(personnelFilesDto);
        if (userIdCount > 0) {
            throw new ScmException(ExceptionEmun.USER_ERROR_USERID);
        }

        if (StringUtils.isEmpty(personnelFilesDto.getUserNo())) {
            dtUserMapper.updateUserSerial();
            Long user_serial = dtUserMapper.getUserSerialAndNo().getModuleUser();
            personnelFilesDto.setUserNo(String.valueOf(user_serial));
            personnelFilesDto.setUserSerial(user_serial);
        } else {
            personnelFilesDto.setUserSerial(Long.parseLong(personnelFilesDto.getUserNo()));
        }

        PersonnelFilesDto depInfo = dtUserMapper.getDepInfo(personnelFilesDto);
        personnelFilesDto.setDepNo(depInfo.getDepNo());
        personnelFilesDto.setDepName(depInfo.getDepName());
        personnelFilesDto.setLogIp(clientIp);
        dtUserMapper.insertTmpDtUserByFace(personnelFilesDto);
        dtUserMapper.insertVacation(personnelFilesDto);
        dtUserMapper.insertDtAcUser(personnelFilesDto);
        dtUserMapper.insertDtAcLink(personnelFilesDto);
        String user_lname = personnelFilesDto.getUserLname();
        String user_no = personnelFilesDto.getUserNo();
        personnelFilesDto.setLogBz("姓名:" + user_lname + "  工号:" + user_no);
        dtUserMapper.insertUserLogByAdd(personnelFilesDto);
        dtUserMapper.insertKqfxRiZhi(personnelFilesDto);
        dtUserMapper.insertWtPublicByAdd(personnelFilesDto);
//        r = 1;
        return personnelFilesDto;
    }

    @Override
    @Transactional
    public int updateUserInfo(PersonnelFilesDto personnelFilesDto, String clientIp) {
        //返回信息字符串
        int r = 0;
        HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();

        JSONObject user = (JSONObject) request.getSession().getAttribute("user");
        String operator =String.valueOf(user.get("userSerial"));
        int isExist = dtUserMapper.isExistUserNo(personnelFilesDto);
        personnelFilesDto.setGlyNo(operator);
        if (isExist <= 0) {
            personnelFilesDto.setLogIp(clientIp);
            //添加人员全年的普通班次 除了封账月份
            personnelFilesDto.setDoType(0);
            personnelFilesDto.setUserSerialStr(personnelFilesDto.getUserSerial()+"");
            dtUserMapper.addScheduleCalendarPuTong(personnelFilesDto);
            dtUserMapper.updatePerson(personnelFilesDto);
            String acType = dtUserMapper.getActypeById(personnelFilesDto);
            personnelFilesDto.setOldAcBh(acType);
            dtUserMapper.updateAcType(personnelFilesDto);
            dtUserMapper.execClientUserLimits(personnelFilesDto);
            dtUserMapper.insertUserLogByUp(personnelFilesDto);
            dtUserMapper.insertWtPublicByUp(personnelFilesDto);
            //计算授乳假
            calculateLactational(personnelFilesDto);
            r = 1;
        }
        return r;
    }

    /**
     * 计算授乳假
     * @param personnelFilesDto
     */
    public void calculateLactational(PersonnelFilesDto personnelFilesDto) {
        Integer lx = personnelFilesDto.getLx();
        if(4!=lx) {
            //判断是否存在数据
            String kqHolidaysTimeId = dtUserMapper.findKqHolidaysTimeId(personnelFilesDto);
            //查看授乳假有效时间内包含多少双休与节假日
            int workCount = dtUserMapper.getWorkCount(personnelFilesDto);
            personnelFilesDto.setWorkCount(workCount);
            if (!StringUtils.isEmpty(kqHolidaysTimeId)) {
                //存在修改
                personnelFilesDto.setKqHolidaysTimeId(kqHolidaysTimeId);
                if(2 == lx || 3 == lx){
                    //设置年假 假期类型21
                    personnelFilesDto.setHoType(21);
                    //添加授乳假时间明细数据
                    dtUserMapper.insertBdKqHolidaysLactational(personnelFilesDto);
                }
                if(1 == lx || 3 == lx){
                    //添加年假时间明细数据 假期类型2
                    personnelFilesDto.setHoType(2);
                    dtUserMapper.insertBdKqHolidaysaAnnual(personnelFilesDto);
                }
                //修改授乳假
                dtUserMapper.updateLactational(personnelFilesDto);
            } else {
                //不存在添加
                if(2 == lx || 3 == lx){
                    //设置授乳假 假期类型20
                    personnelFilesDto.setHoType(20);
                    //添加授乳假时间明细数据
                    dtUserMapper.insertBdKqHolidaysLactational(personnelFilesDto);
                }
                if(1 == lx || 3 == lx){
                    //添加年假时间明细数据 假期类型2
                    personnelFilesDto.setHoType(2);
                    dtUserMapper.insertBdKqHolidaysaAnnual(personnelFilesDto);
                }
                //添加授乳假
                dtUserMapper.insertBdKqHolidaysTime(personnelFilesDto);
            }
        }
    }

    @Override
    @Transactional
    public int quitUserInfo(String  userSerial, String clientIp) {
        int r = 0;
        PersonnelFilesDto u = new PersonnelFilesDto();
        String[] split = userSerial.split(",");
        for (String s : split) {
            u.setUserSerial(Long.valueOf(s));
            PersonnelFilesDto j = dtUserMapper.queryPersonById(u);
            String user_lname = j.getUserLname();
            String user_no = j.getUserNo();
            int i = dtUserMapper.quitPerson(u);
            if (i > 0) {
                u.setLogBz("姓名:" + user_lname + "  工号:" + user_no);
                u.setLogIp(clientIp);
                dtUserMapper.insertUserLogByQuit(u);
                dtUserMapper.insertWtPublicByQuit(u);
                r = 1;
            }
        }

        return r;
    }

    @Override
    public int addLoanInfo(PersonnelFilesDto j, String clientIp) {
        int r = 0;
        j.setGlyNo("super");
        int i = dtUserMapper.addLoanInfo(j);
        if (i > 0) {
            return 1;
        } else {
            throw new ScmException(ExceptionEmun.USER_ERROR_LOAN);
        }

    }

    /**
     * 永久调用
     * @param beginTime 调动日期
     * @param userSerial 调动人
     * @param depSerialNew 新部门
     * @param glyNo 操作人
     * @param clientIp
     * @param gly 操作
     * @return
     */
    @Override
    @Transactional
    public String transferPerson(String beginTime,Long userSerial,String depSerialNew,String glyNo, String clientIp,String gly) {
        String currentDate = DateUtils.getCurrentDate();
        String r = null;
        String ser = String.valueOf(userSerial);
        String[] split = ser.split(",");
        PersonnelFilesDto personnelFilesDto = new PersonnelFilesDto();
        for (String s : split) {
            Long ss = Long.parseLong(s);
            personnelFilesDto.setUserSerial(ss);
            personnelFilesDto.setDepSerialNew(depSerialNew);
            personnelFilesDto.setLogIp(clientIp);
            PersonnelFilesDto depInfo= dtUserMapper.getDepInfoNew(personnelFilesDto);
            personnelFilesDto.setDepNo(depInfo.getDepNo());
            personnelFilesDto.setDepName(depInfo.getDepName());
            PersonnelFilesDto j = dtUserMapper.queryPersonById(personnelFilesDto);
            String user_lname = j.getUserLname();
            String user_no = j.getUserNo();
            String depSerial = dtUserMapper.getDepSerial(personnelFilesDto);
            personnelFilesDto.setDepSerial(depSerial);
            //判断调用日期是否是当天
            if(currentDate.equals(beginTime)) {
                dtUserMapper.realUserRemove(personnelFilesDto);
                int i = dtUserMapper.transferUser(personnelFilesDto);
                if(i>0){
                    personnelFilesDto.setLogBz("姓名"+user_lname+"工号:"+user_no+"从"+personnelFilesDto.getDepName()+"调动到"+depInfo.getDepSerialNew());
                    dtUserMapper.insertTransferWt(personnelFilesDto);
//                    dtUserMapper.insertTransferLog(personnelFilesDto);
                }else{
                    r+=user_lname+"调动失败";
                }
            }
            //判断不是定时任务执行的
            if("1".equals(gly)){
                //添加部门调动bd_temporary_loan
                TransferUser transferUser = new TransferUser();
                transferUser.setModel("1");
                transferUser.setUserSerial(String.valueOf(personnelFilesDto.getUserSerial()));
                transferUser.setDepSerial(personnelFilesDto.getDepSerial());
                transferUser.setGlyNo(glyNo);
                transferUser.setBeginTime(beginTime);
                this.transferUserMapper.addLoanInfo(transferUser);
            }
        }
        return r;
    }

    @Override
    public DtUser getUserInfoByCardId(String userId) {

        DtUser user = dtUserMapper.selectOne(new QueryWrapper<DtUser>().eq("user_id", userId));
        if (user == null) throw new ScmException(ExceptionEmun.USER_ERROR_NOTFOUND);

        DtDepCust cust = iDtDepCustService.getByUserId(user.getUserSerial());
        if (cust == null) throw new ScmException(ExceptionEmun.USER_ERROR_NOTDEPTMANAGER);

        return user;
    }

    @Override
    public PersonnelFilesDto getUserSerialAndNo() {
        dtUserMapper.updateUserSerial();
        return dtUserMapper.getUserSerialAndNo();
    }

    @Override
    public List<PersonnelFilesDto> getacTypeList() {
        return dtUserMapper.getacTypeList();
    }

    @Override
    public List<PersonnelFilesDto> getNation() {
        return dtUserMapper.getNation();
    }

    @Override
    public List<PersonnelFilesDto> getNative() {
        return dtUserMapper.getNative();
    }

    @Override
    public List<PersonnelFilesDto> getUserType() {
        return dtUserMapper.getUserType();
    }


    @Override
    public List<PersonnelFilesDto> getkqdkLxList() {
        return dtUserMapper.getkqdkLxList();
    }

    @Override
    public List<PersonnelFilesDto> getjcLxList() {
        return dtUserMapper.getjcLxList();
    }

    @Override
    @Transactional
    public String uploadingExcel(MultipartFile file, HttpServletRequest request) throws Exception {
        String r = null;
        String clientIp = InetAddress.getLocalHost().getHostAddress();
        DateFormat df = new SimpleDateFormat("yyyyMMddHHmmssSS");
        Calendar calendar = Calendar.getInstance();
        String bh = df.format(calendar.getTime()).substring(0, 16);
        String tableName = "Import_user_" + bh;
        dtUserMapper.createLsTable(tableName);
        InputStream inputStream = new BufferedInputStream(file.getInputStream());
//        InputStream inputStream = file.getInputStream();
        Integer startLineNum = 2;
        List<PersonnelFilesImport> list = ExcelUtil.ImportExcel(inputStream, PersonnelFilesImport.class,startLineNum);
        try {
            for (PersonnelFilesImport personnelFiles : list) {
                personnelFiles.setLogIp(clientIp);
                personnelFiles.setGlyNo("super");
                personnelFiles.setTableName(tableName);
                personnelFiles.setRowNum(startLineNum);
                Long user_serial = dtUserMapper.getUserSerial();
                personnelFiles.setUserSerial(user_serial);
                int i = dtUserMapper.vacationIsExist(personnelFiles);
                if (i == 0) {
                    dtUserMapper.insertVacationByimport(personnelFiles);
                }
                dtUserMapper.addUserLinShi(personnelFiles);
            }
            PersonnelFilesImport personnelFiles = new PersonnelFilesImport();
            personnelFiles.setLogIp(clientIp);
            personnelFiles.setGlyNo("super");
            personnelFiles.setTableName(tableName);
            dtUserMapper.execImportUser(personnelFiles);
            startLineNum++;
        } catch (Exception e) {
            r = "导入数据有误!";
            e.printStackTrace();
        }

        return r;
    }

    @Override
    public String userSynchronization() throws Exception {
        String r = null;
        try {
            DateFormat df = new SimpleDateFormat("yyyyMMddHHmmssSS");
            Calendar calendar = Calendar.getInstance();
            String bh = df.format(calendar.getTime()).substring(0, 16);
            String tableName = "syn_user_" + bh;
            dtUserMapper.createLsTableBySyn(tableName);
            String clientIp = InetAddress.getLocalHost().getHostAddress();
            PersonnelFilesImport user = new PersonnelFilesImport();
            user.setLogIp(clientIp);
            user.setGlyNo("super");
            user.setTableName(tableName);
            dtUserMapper.addSynchronizationLinShi(user);
            dtUserMapper.execSynUser(user);
            return null;
        } catch (Exception e) {
            e.printStackTrace();
            throw new ScmException(ExceptionEmun.SERVICE_ERROR);
        }
    }

    @Override
    public String deptSynchronization() throws Exception {
        try {
            dtUserMapper.execAddToTmp();
            dtUserMapper.execSynDep();
            return null;
        } catch (Exception e) {
            e.printStackTrace();
            throw new ScmException(ExceptionEmun.SERVICE_ERROR);
        }
    }

    @Override
    public int manualSynUserInfo(String  userNo, String clientIp) {
        int n = 0;
        String[] split = userNo.split(",");
        PersonnelFilesDto personnelFilesDto = new PersonnelFilesDto();
        for (String s : split) {
            personnelFilesDto.setUserNo(s);
            //保存消费时段与班次
            n+=this.dtUserMapper.manualSynUserInfo(personnelFilesDto);
        }
        return n;
    }

    /**
     * 根据登录人获取第一级审批人（批量、导入）.
     *
     * @return
     */
    @Override
    public List<PersonnelFilesDto> getApproverListByBatch(String userSerial) {

        List<PersonnelFilesDto> list = new ArrayList<>();
        String depNo = this.dtUserMapper.findDepParentID2(userSerial);
        if(!StringUtils.isEmpty(depNo)){
            list = dtUserMapper.getFirstListByBatch(depNo);
        }
        return list;
    }
    /**
     * 根据登录人获取第二级审批人（批量、导入）.
     *
     * @return
     */
    @Override
    public List<PersonnelFilesDto> getSecondListByBatch(String userSerial) {

        List<PersonnelFilesDto> list = new ArrayList<>();
        String depNo = this.dtUserMapper.findDepParentID3(userSerial);
        if(!StringUtils.isEmpty(depNo)){
            list = dtUserMapper.getSecondListByBatch(depNo);
        }
        return list;
    }
    /**
     * 根据登录人获取第一级审批人(个人).
     *
     * @return
     */
    @Override
    public List<PersonnelFilesDto> getApproverList(String userSerial) {
        return dtUserMapper.getApproverList(userSerial);
    }

    /**
     * 根据登录人获取第二级审批人 个人.
     *
     * @return
     */
    @Override
    public List<PersonnelFilesDto> getLabourUnionApproverList(String userSerial) {
        List<PersonnelFilesDto> labourUnionApproverList = new ArrayList<>();
        String depNo = this.dtUserMapper.findDepParentID(userSerial);
        if(!StringUtils.isEmpty(depNo)){
            labourUnionApproverList = dtUserMapper.getLabourUnionApproverList(depNo);
        }
        return labourUnionApproverList;
    }

    /**
     * 根据人员ID查看该人员是否在生产制造部中.
     *
     * @param userSerial
     * @return
     */
    @Override
    public Integer getUserCountById(String userSerial) {
        return dtUserMapper.getUserCountById(userSerial);
    }

    @Override
    public void manualSynUser() {
        dtUserMapper.manualSynUser();
    }

    /**
     * 自动同步人员数据
     */
    @Override
    public void selfMotionSynUser() {
        try {
            //创建数据库服务
//            this.dtUserMapper.execStartUsing();
            //自动同步人员数据
            this.dtUserMapper.execSelfMotionsynuser();
            //关闭数据库服务
//            this.dtUserMapper.execClose();
        } catch (Exception e) {
            e.printStackTrace();
            throw new ScmException(ExceptionEmun.SERVICE_ERROR);
        }
    }

    /**
     * 自动同步部门数据
     */
    @Override
    public void selfMotionSynDep() {
        try {
            //创建数据库服务
//            this.dtUserMapper.execStartUsing();
            //将数据添加到部门临时表中
            this.dtUserMapper.execSelfMotionaddDataToTmpDep();
            //关闭数据库服务
//            this.dtUserMapper.execClose();
            //同步部门数据
            this.dtUserMapper.execSynDep();
        } catch (Exception e) {
            e.printStackTrace();
            throw new ScmException(ExceptionEmun.SERVICE_ERROR);
        }
    }

    /**
     * 获取所属公司集合.
     *
     * @return
     */
    @Override
    public List<String> getUserCompany() {
        return dtUserMapper.getUserCompany();
    }

    /**
     * 获取员工类型集合.
     *
     * @return
     */
    @Override
    public List<String> getUserSnyLx() {
        return dtUserMapper.getUserSnyLx();
    }

    /**
     * 获取员工状态集合.
     *
     * @return
     */
    @Override
    public List<String> getUserSnyType() {
        return dtUserMapper.getUserSnyType();
    }

    /**
     * 查询所有工作日历
     * @return
     */
    @Override
    public List<JSONObject> findBdKqWorkCalendarList() {
        return this.dtUserMapper.findBdKqWorkCalendarList();
    }

    /**
     * 部门人员设置工作日历
     * @param personnelFilesDto
     * @return
     */
    @Override
    @Transactional
    public String setUserCalendar(PersonnelFilesDto personnelFilesDto) {
        //返回信息字符串
        String r = null;
        try {
            //根据部门ID查询人员ID
            List<String> userSerialList = this.dtUserMapper.getUserSerialList(personnelFilesDto);
            if(userSerialList.size()>0){
                String join = StringUtils.join(userSerialList, ",");
                System.out.println(join);
                //根据部门ID批量修改人的工作日历
                personnelFilesDto.setDoType(1);
                personnelFilesDto.setUserSerialStr(join);
                dtUserMapper.addScheduleCalendarPuTong(personnelFilesDto);
            }
        } catch (Exception e) {
            e.printStackTrace();
            r = "批量设置工作日历失败";
        }
        return r;
    }

    /**
     * 获取职务列表.
     *
     * @return
     */
    @Override
    public List<String> getJobGrade() {
        return dtUserMapper.getJobGrade();
    }

    /**
     * 获取岗位级别列表.
     *
     * @return
     */
    @Override
    public List<String> getTechnologicalJob() {
        return dtUserMapper.getTechnologicalJob();
    }

    /**
     * 根据微信账号是否能查询到信息
     * @param weChatId
     * @return
     */
    @Override
    public int fingUserWxCount(String weChatId) {
        return dtUserMapper.fingUserWxCount(weChatId);
    }

    /**
     * 根据用户名跟密码获取用户信息
     * @param account 工号
     * @param psw 密码
     * @param systemType
     * @param weChatId 微信账号
     * @return
     */
    @Override
    @Transactional
    public JSONObject getwXUserByAccountAndPsw(String account, String psw, String systemType, String weChatId,HttpServletRequest request) {
        JSONObject result = new JSONObject();
        JSONObject object = new JSONObject();
        object.put("account",account);
        object.put("psw",psw);
        result.put("success",true);
        result.put("resultCode","200");
        result.put("message","成功");
        result.put("content","");
        try {
            if (StringUtils.isBlank(account) || StringUtils.isBlank(psw)) {
                result.put("success",false);
                result.put("resultCode","10002");
                result.put("message","用户名或密码为空");
                return result;
            }
            if(systemType!=null && systemType !="" && systemType.equals("6")){
                int count=this.dtUserMapper.getUserByAccountAndPswCount(object);
                if(count==0){
                    result.put("success",false);
                    result.put("resultCode","10003");
                    result.put("message","抱歉！您没有登录权限");
                    return result;
                }
            }
            JSONObject u = this.loginMapper.getUser(account);
            if (null != u) {
                request.getSession().setAttribute("user",u);
                if("".equals(u.getString("weChatId")) || u.getString("weChatId")==null){
                    result.put("content",u);
                    if(!"".equals(weChatId) && weChatId!=null){
                        object.put("weChatId",weChatId);
                        //添加企业微信ID
                        int b = this.dtUserMapper.updateUser(object);
                        if(b>0){
                            log.info("工号:"+account+"企业微信绑定成功 微信账号:" +weChatId);
                            System.out.println("工号:"+account+"企业微信绑定成功 微信账号:" +weChatId);
                        }else{
                            log.info("工号:"+account+"企业微信绑定失败 微信账号:" +weChatId);
                            System.out.println("工号:"+account+"企业微信绑定失败 微信账号:" +weChatId);
                        }
                    }else{
                        log.info("未获取微信账号");
                        System.out.println("未获取微信账号");
                    }
                }else {
                    result.put("success",false);
                    result.put("resultCode","10005");
                    result.put("message","抱歉！该用户已绑定");
                    return result;
                }
            } else {
                result.put("success",false);
                result.put("resultCode","10004");
                result.put("message","抱歉！用户账号密码不匹配");
                return result;
            }
        } catch (Exception e) {
            e.printStackTrace();
            throw e;
        }
        return result;
    }


    /**
     * 根据企业微信账号获取人员信息
     * @param weChatId 企业微信账号
     * @return
     */
    @Override
    @Transactional
    public JSONObject getUserByWeChatId(String weChatId,HttpServletRequest request) {
        JSONObject result = new JSONObject();
        result.put("success",true);
        result.put("resultCode","200");
        result.put("message","成功");
        result.put("content","");
        JSONObject u = this.loginMapper.getUserByWeChatId(weChatId);
        if (null != u) {
            result.put("content",u);
            request.getSession().setAttribute("user",u);
        } else {
            result.put("success",false);
            result.put("resultCode","10004");
            result.put("message","抱歉！企业微信账号不匹配");
        }
        return result;
    }

    /** 根据人员ID查询人员信息
     * @param  userId
     * @return
     */
    @Override
    @Transactional
    public JSONObject getUserByUserId(String userId,HttpServletRequest request) {
        JSONObject result = new JSONObject();
        result.put("success",true);
        result.put("resultCode","200");
        result.put("message","成功");
        result.put("content","");
        JSONObject u = this.loginMapper.getUserByUserId(userId);
        if (null != u) {
            result.put("content",u);
            request.getSession().setAttribute("user",u);
        } else {
            result.put("success",false);
            result.put("resultCode","10004");
            result.put("message","抱歉！企业微信账号不匹配");
        }
        return result;
    }

    /**
     * 根据人员id获取该人员部门下的审批人，即为二级审批人
     *
     * @param userSerial
     * @return
     */
    @Override
    public List<String> getDeptCustByUserId(Long userSerial) {
        return dtUserMapper.getDeptCustByUserId(userSerial);
    }

    /**
     * 根据人员ID修改人员表中人脸信息，有2  无0
     * * @param userSerial
     *
     * @param userSerial
     */
    @Transactional
    @Override
    public void updateUserFaceById(Long userSerial, Integer faceState) {
        dtUserMapper.updateUserFaceById(userSerial, faceState);
    }

    /**
     *
     * @param object
     */
    @Transactional
    @Override
    public void insertBdFaceUploadLog(JSONObject object) {
        dtUserMapper.insertBdFaceUploadLog(object);
    }

    /**
     * 更新人脸上传日期
     * @param object
     */
    @Override
    public void updateUserRlDate(JSONObject object) {
        dtUserMapper.updateUserRlDate(object);
    }
}
